﻿
#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式



#include "RootWindow.h"
#include "Util/GDStatisticsUtil.h"
#include "Util/GDUtil.h"
#include "Util/GumboUtil.h"
#include "CrossAppExt.h"
#include "DataModule/ConfigDataModule.h"
#include "FirstViewController.h"

#ifndef WIN32

#define stricmp strcasecmp
#endif


//#include "TestMainViewController.h"

static RootWindow* _window = NULL;


RootWindow* RootWindow::create()
{
	_window = new RootWindow();
	_window->init();
	_window->autorelease();
	return _window;
}

RootWindow* RootWindow::getInstance()
{
	if (_window == NULL)
	{
		_window = new RootWindow();
		_window->init();
		_window->autorelease();
	}
	return _window;

}

RootWindow::RootWindow()
: m_pRootTabBarController(NULL)
, m_pRootDrawerController(NULL)
, m_bAlertShow(false)
, leftMenuViewController(NULL)
, networkView(NULL)
{
	CAApplication::getApplication()->getKeypadDispatcher()->addDelegate(this);
}

RootWindow::~RootWindow()
{

}

void RootWindow::initExternConst()
{
	APP_NAME = ConfigDataModule::getInstance()->getSource("appName", "A佳购物");
	APP_NAME_SHORT = ConfigDataModule::getInstance()->getSource("appNameShort", "A佳");
	/* 是否是debug模式 */
	std::string isDebug = ConfigDataModule::getInstance()->getSource("is_debug", "false");

	CCSize winsize = this->getFrame().size;

	if (winsize.width == 640 && winsize.height == 1136){
		FONT_SCALE = 0;//_ds(3);
	}

	if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || stricmp(isDebug.c_str(), "true") == 0){
		IS_DEBUG = true;
	}

	// 版本信息
	std::string version = CADevice::getAppVersion();
	if (version.empty() || version == ""){
		version = CrossAppVersion();
	}
	APP_VER = version;

	GD_DOMAIN = ConfigDataModule::getInstance()->getSource("gd_domain", "http://www.geelou.com/geelouwx/");
}

bool RootWindow::init()
{
	if (!CAWindow::init())
	{
		return false;
	}

	GDStatisticsUtil::initUMStatistics();

	design_scale = this->getFrame().size.width / design_size.width;


	//GumboUtil::test();

	// 加载配置数据
	ConfigDataModule::getInstance()->init();
	initExternConst();

	//if (IS_DEBUG) CCLog("------------getAngle：%f", GDUtil::getAngle(1, -1));

	CANavigationController *nav = new CANavigationController();
	CAViewController *first = NULL;

	bool isGuide = CAUserDefault::sharedUserDefault()->getBoolForKey("isGuide", true);
	
	first = new FirstViewController();
	
	first->init();
	first->autorelease();
	nav->initWithRootViewController(first);
	CANavigationBarItem *item = CANavigationBarItem::create("首页");
	first->setNavigationBarItem(item);
	//nav->setNavigationBarHidden(true, false);

	leftMenuViewController = LeftMenuViewController::create();

	CADrawerController* drawer = new CADrawerController();
	drawer->initWithController(leftMenuViewController, nav, this->getBounds().size.width * 5 / 6.0f);

	CAImageView *bgCAImageView = CAImageView::createWithImage(CAImage::create(GD_IMAGE_PATH + "left_bg.jpg"));
	bgCAImageView->addSubview(CAView::createWithFrame(this->getBounds(), ccc4(0, 0, 0, 255 * 0.75)));

	drawer->setBackgroundView(bgCAImageView);
	drawer->setEffect3D(true);

	nav->release();

	this->setRootViewController(drawer);

	drawer->release();

	m_pRootNavigationController = nav;
	//m_pRootTabBarController = tabBar;
	m_pRootDrawerController = drawer;

	return true;
}

void RootWindow::keyBackClicked()
{
	if (IS_DEBUG) CCLog("RootWindow keyBackClicked");
	if (!keyBackEnable){
		return;
	}

	GDUtil::closeViewController();
}

void RootWindow::showExitAlertView(){
	CC_RETURN_IF(m_bAlertShow);
	m_bAlertShow = true;
	keyBackEnable = false;
	GDUtil::confirm("确定要退出？", this, CAAlertView_selector(RootWindow::onAlertView));
}

void RootWindow::keyMenuClicked()
{

}

void RootWindow::onAlertView(int index)
{
	if (index == 1)
	{
		CAApplication::getApplication()->end();
	}
	m_bAlertShow = false;
	keyBackEnable = true;
}

void RootWindow::onTokenTimeout(int index){
	
}
